﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;

namespace YArchitech.Revit
{
	public class YJKElementFilter
	{
		public static List<Element> GetElementByCategory(BuiltInCategory builtInCategory, Document doc)
		{
			return new FilteredElementCollector(doc).OfCategory(builtInCategory).OfClass(typeof(MechanicalSystemType)).ToElements().ToList<Element>();
		}

		public static List<Element> GetElementByCategory(Document doc, BuiltInCategory builtInCategory)
		{
			return new FilteredElementCollector(doc).OfCategory(builtInCategory).ToElements().ToList<Element>();
		}

		public static List<Element> GetElementByCategory(Document doc, Type type, BuiltInCategory builtInCategory)
		{
			return new FilteredElementCollector(doc).OfClass(type).OfCategory(builtInCategory).ToElements().ToList<Element>();
		}

		public static List<Element> GetElementByClass(Type type, Document doc, ElementId levelId)
		{
			List<Element> list = new List<Element>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(type).ToElements().ToList<Element>();
			foreach (Element element in filteredElementCollector)
			{
				if (element.get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId() == levelId && !element.IsHidden(doc.ActiveView))
				{
					list.Add(element);
				}
			}
			return list;
		}

		public static List<Element> GetElementByClass(Type type, Document doc)
		{
			return new FilteredElementCollector(doc).OfClass(type).ToElements().ToList<Element>();
		}

		public static List<Element> GetElementByClass(Document doc, Type type)
		{
			return new FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(type).ToElements().ToList<Element>();
		}

		public static List<string> GetElementNameByClass(Type type, Document doc)
		{
			List<Element> elementByClass = YJKElementFilter.GetElementByClass(type, doc);
			List<string> list = new List<string>();
			foreach (Element element in elementByClass)
			{
				if (element != null)
				{
					list.Add(element.Name);
				}
			}
			return list;
		}

		public static FilteredElementCollector GetElementsOfType(Document doc, Type type, BuiltInCategory bic)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfCategory(bic);
			filteredElementCollector.OfClass(type);
			return filteredElementCollector;
		}

		public static List<Element> GetElementsOfType(Document doc, Type type, BuiltInCategory bic, ElementId levelId)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(type);
			filteredElementCollector.OfCategory(bic);
			List<Element> list = new List<Element>();
			foreach (Element element in filteredElementCollector)
			{
				if (element.get_Parameter(BuiltInParameter.RBS_START_LEVEL_PARAM).AsElementId() == levelId)
				{
					list.Add(element);
				}
			}
			return list;
		}
	}
}
